﻿using BasicHostModule.Module;
using Karambolo.Extensions.Logging.File;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Hosting;
using System.Text;

namespace BasicHostModule
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 运行console -- .netcore2.2
            {
                //    // 1、创建IHostBuilder
                //    IHostBuilder builder = Host.CreateDefaultBuilder(args);
                //    //2、创建泛型主机
                //    IHost host = builder.Build();
                //    //3、运行console
                //    host.Run();
            }
            #endregion

            #region 运行console-.NET5 .NET8
            {
                // 1、创建HostApplicationBuilder
                HostApplicationBuilder hostApplicationBuilder = Host.CreateApplicationBuilder(args);
                // 2、创建泛型主机
                IHost host = hostApplicationBuilder.Build();
                // 3、运行console
                host.Run();
            }
            #endregion

            #region 运行web-.NET5 .NET8
            {
                //// 1、创建WebApplicationBuilder
                //WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder(args); 
                ////WebApplication webApplication = webApplicationBuilder.Build();
                //// 2、创建泛型主机
                //IHost host = webApplicationBuilder.Build();
                //// 3、运行console
                //host.Run();
            }
            #endregion

            #region 泛型主机运行/管理应用程序 .NET5 .NET8  集成IOC 、Logging 、 Configuration 后台任务 IHostedService BackgroundService
            {
                //// 1、创建WebApplicationBuilder
                //HostApplicationBuilder hostApplicationBuilder = Host.CreateApplicationBuilder(args); 
                //hostApplicationBuilder.Services.AddTransient<ProductService>();
                //hostApplicationBuilder.Configuration.AddJsonFile("appsettings.json");
                //hostApplicationBuilder.Configuration.AddXmlFile("appsettings.xml");
                //hostApplicationBuilder.Logging.ClearProviders();
                ////var xx = hostApplicationBuilder.Configuration.GetSection("Logging:LogLevel:Default").;
                //hostApplicationBuilder.Services.AddLogging(x => {
                //    x.AddConfiguration(hostApplicationBuilder.Configuration.GetSection("Logging"));
                //    x.AddLog4Net("log4Net.config");
                //    x.AddConsole();
                //});
                ////hostApplicationBuilder.Logging.AddConsole();
                ////hostApplicationBuilder.Logging.AddLog4Net("log4Net.config");
                ////hostApplicationBuilder.Logging.AddFile(builder =>
                ////{
                ////    builder.RootPath = AppContext.BaseDirectory;
                ////    //builder.BasePath = AppContext.BaseDirectory;
                ////    builder.Files = new[] {
                ////        new LogFileOptions
                ////        {
                ////            Path = $"logs/myapp-",
                ////            MaxFileSize = 10485760
                ////        }
                ////    };

                ////});
                ////创建泛型主机
                //IHost host = hostApplicationBuilder.Build();
                ////获取ProductService
                //ProductService productService = host.Services.GetRequiredService<ProductService>();
                //productService.Run();
                ////运行console
                //host.Run();
            }
            #endregion

            #region 泛型主机运行/管理应用程序 .NET5 .NET8  后台任务 IHostedService BackgroundService
            {
                //1、创建WebApplicationBuilder
                HostApplicationBuilder hostApplicationBuilder = Host.CreateApplicationBuilder(args);
                hostApplicationBuilder.Services.AddTransient<ProductService>();
                hostApplicationBuilder.Services.AddHostedService<MyHostedService>();   //Net5 之后
                hostApplicationBuilder.Services.AddHostedService<TaskBackgroundService>();  //Net5 之前
                hostApplicationBuilder.Configuration.AddJsonFile("appsettings.json");
                hostApplicationBuilder.Configuration.AddXmlFile("appsettings.xml");
                hostApplicationBuilder.Logging.ClearProviders();
                hostApplicationBuilder.Services.AddLogging(x =>
                {
                    x.AddConfiguration(hostApplicationBuilder.Configuration.GetSection("Logging"));
                    x.AddLog4Net("log4Net.config");
                    x.AddConsole();
                });
                //创建泛型主机
                IHost host = hostApplicationBuilder.Build();
                //运行console
                host.Run();
                Console.WriteLine("xxx");
            }
            #endregion

            #region 泛型主机运行/管理应用程序 .NET5 .NET8  后台任务 IHostedService 应用程序生命周期函数 IHostApplicationLifetime
            {
                // 1、创建WebApplicationBuilder
                HostApplicationBuilder hostApplicationBuilder = Host.CreateApplicationBuilder(args);
                hostApplicationBuilder.Services.AddTransient<ProductService>();
                hostApplicationBuilder.Services.AddHostedService<MyHostedService>();   //Net5 之后
                //hostApplicationBuilder.Services.AddHostedService<TaskBackgroundService>();  //Net5 之前
                hostApplicationBuilder.Configuration.AddJsonFile("appsettings.json");
                hostApplicationBuilder.Configuration.AddXmlFile("appsettings.xml");
                hostApplicationBuilder.Services.AddStackExchangeRedisCache(x => x.Configuration = "localhost:6379");
                hostApplicationBuilder.Logging.ClearProviders();
                hostApplicationBuilder.Services.AddLogging(x =>
                {
                    x.AddConfiguration(hostApplicationBuilder.Configuration.GetSection("Logging"));
                    x.AddLog4Net("log4Net.config");
                    x.AddConsole();
                });

                //创建泛型主机
                IHost host = hostApplicationBuilder.Build();
                IHostApplicationLifetime lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
                lifetime.ApplicationStarted.Register((serverProvider) =>
                {
                    var xx = serverProvider as ServiceProvider;
                    var ww = xx.GetRequiredService<IDistributedCache>();
                    ww.Set("test_key", Encoding.UTF8.GetBytes("xxx"));
                    Console.WriteLine("周期函数开始执行完成");
                }, host.Services);
                lifetime.ApplicationStopped.Register((serverProvider) =>
                {
                    var xx = serverProvider is ServiceProvider;
                    Console.WriteLine("周期函数开始关闭完成");
                }, host.Services);
                lifetime.ApplicationStopping.Register((serverProvider) =>
                {
                    var xx = serverProvider is ServiceProvider;
                    Console.WriteLine("周期函数开始关闭中");
                }, host.Services);
                //运行console
                host.Run();
                Console.WriteLine("xxx");
            }
            #endregion

            #region 泛型主机运行/管理应用程序 .NET5 .NET8  后台任务 IHostedService 环境变量的使用 
            {
                //// 1、创建WebApplicationBuilder
                //Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", "Development");
                //HostApplicationBuilder hostApplicationBuilder = Host.CreateApplicationBuilder(args);
                //hostApplicationBuilder.Services.AddTransient<ProductService>();
                //hostApplicationBuilder.Services.AddHostedService<MyHostedService>();   //Net5 之后
                ////hostApplicationBuilder.Services.AddHostedService<TaskBackgroundService>();  //Net5 之前
                //if (hostApplicationBuilder.Environment.EnvironmentName == "Development")
                //{
                //    hostApplicationBuilder.Configuration.AddJsonFile("appsettings.json");
                //}
                //else {
                //    hostApplicationBuilder.Configuration.AddJsonFile("appsettings-test.json");
                //}
                //hostApplicationBuilder.Configuration.AddXmlFile("appsettings.xml");
                //hostApplicationBuilder.Logging.ClearProviders();
                //hostApplicationBuilder.Services.AddLogging(x =>
                //{
                //    x.AddConfiguration(hostApplicationBuilder.Configuration.GetSection("Logging"));
                //    x.AddLog4Net("log4Net.config");
                //    x.AddConsole();
                //});
                ////创建泛型主机
                //IHost host = hostApplicationBuilder.Build();

                ////运行console
                //host.Run();
                //Console.WriteLine("xxx");
            }
            #endregion

            #region 泛型主机运行/管理应用程序 .NET5 .NET8   Console项目web化  将Console项目转化为Web项目
            {
                // 1、创建WebApplicationBuilder
                Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", "Development");
                WebApplicationBuilder hostApplicationBuilder = WebApplication.CreateBuilder(args);
                hostApplicationBuilder.Services.AddTransient<ProductService>();
                hostApplicationBuilder.Services.AddHostedService<MyHostedService>();   //Net5 之后
                //hostApplicationBuilder.Services.AddHostedService<TaskBackgroundService>();  //Net5 之前
                if (hostApplicationBuilder.Environment.EnvironmentName == "Development")
                {
                    hostApplicationBuilder.Configuration.AddJsonFile("appsettings.json");
                }
                else
                {
                    hostApplicationBuilder.Configuration.AddJsonFile("appsettings-test.json");
                }

                hostApplicationBuilder.Configuration.AddXmlFile("appsettings.xml");
                hostApplicationBuilder.Logging.ClearProviders();
                hostApplicationBuilder.Services.AddLogging(x =>
                {
                    x.AddConfiguration(hostApplicationBuilder.Configuration.GetSection("Logging"));
                    x.ClearProviders();
                    x.AddLog4Net("log4Net.config");
                    x.AddConsole();
                });
                //创建泛型主机
                IHost host = hostApplicationBuilder.Build();

                //运行console
                host.Run();
                Console.WriteLine("xxx");
            }
            #endregion
        }
    }
}
